const cheerio = require('cheerio');
const request = require('request');
const EasyFtp = require('easy-ftp');
const fs = require('fs');
const rm = require('./lib/rm');

const url = 'http://www.nmc.cn/publish/typhoon/message.html';
const ids = new Set();
const client = new EasyFtp();

let readyCount = 0;
let finishCount = 0;
let runCount = 0;

// 这里是可以编辑的内容
/** 两次采集之间的间隔时间（毫秒） */
const delay = 120000;
/** 发布路径，按照下面的配置会发布到/home/cimstech/MeteoroinfoicalInfo_GDEPEI/xz/typhoon文件夹。 */
const uploadPath = '/home/cimstech/MeteoroinfoicalInfo_GDEPEI/xz';
/** FTP服务器配置 */
const ftpConfig = {
    /** 服务器地址 */
    host: '192.168.0.213',
    /** 端口号 */
    port: 22,
    /** ftp服务类型，可以是ftp或sftp */
    type: 'sftp',
    /** 用户名 */
    username: 'cimstech',
    /** 器密码 */
    password: 'cims#0'
};

// 到此为止，下面不要再编辑了。

function cd(path) {
    return new Promise((resolve, reject) => {
        client.cd(path, (err, path2) => {
            if (err) {
                console.info(`不存在路径${path}，具体报错信息：${err}`);
                console.info(`尝试创建路径${path}`);
                client.mkdir(path, err => {
                    if (err) {
                        reject(err);
                    } else {
                        client.cd(path, err => {
                            if (err) {
                                reject(err);
                            } else {
                                resolve();
                            }
                        });
                    }
                });
            } else {
                resolve();
            }
        });
    });
}

function upload(localPath, remotePath) {
    return new Promise((resolve, reject) => {
        client.upload(localPath, remotePath, (err) => {
            if (err) {
                reject(err);
            } else {
                resolve();
            }
        });
    });
}

function publishFile() {
    //서버 접속(connect) 
    console.log(`尝试连接ftp服务器。服务器地址：${ftpConfig.host} 类型：${ftpConfig.type}`);
    client.connect(ftpConfig);
    console.log('连接成功，尝试切换到/MeteoroinfoicalInfo_GDEPEI/xz/typhoon 目录...');
    cd(uploadPath).then(() => {
        return upload('typhoon', '.');
    }).catch(err => {
        console.log(err);
    }).then(() => {
        client.close();
    }).catch(() => {
        console.log(err);
        client.close();
    });

    setTimeout(run, delay);
};

const getDataIds = function (url) {
    return new Promise((resolve, reject) => {
        console.info(`对 ${url} 发起请求，获取报文ID列表。`);
        let cache = copySet(ids);
        ids.clear();
        request(url, { method: 'GET' }, (error, response, body) => {

            if (error) {
                reject(error);
            } else if (!body) {
                reject(`${url}响应没有正文`);
            } else {
                console.info(`${url} 已响应...`);
                let newIds = new Set();
                let $ = cheerio.load(body);
                $('#mylistcarousel').find('> li > p.time').each((i, ele) => {
                    let id = $(ele).data('id');
                    ids.add(id);
                    if (!cache.has(id)) {
                        newIds.add(id);
                    }
                });
                if (newIds.size > 0) {
                    console.info(`本次总计获取到 ${newIds.size} 条新报文。`)
                } else {
                    reject('没有获取到新报文，本次运行结束！');
                }
                resolve(newIds);
            }
        });
    });
};

const readTyphoonData = function (id) {
    let url = `http://www.nmc.cn/f/rest/getContent?dataId=${id}`;
    console.info(`对${url}发起请求...`);
    request({
        url: url,
        method: 'GET'
    }, (error, response, body) => {
        finishCount++;
        if (error) {
            console.warn(error);
        } else if (!body) {
            console.warn(`${url}响应没有正文`);
        } else {
            console.info(`${url}请求...已响应`);
            let $ = cheerio.load(body);
            let text = $('p').text();
            fs.writeFileSync(`tmp/${id}.txt`, text);
        }
        if (finishCount >= readyCount) {
            console.info('所有报文读取完毕，开始上传至FTP服务器');
            publishFile();
        }
    });
};

const copySet = function (oldSet) {
    let newSet = new Set();
    oldSet.forEach((t) => {
        newSet.add(t);
    });
    return newSet;
};

const run = function () {
    console.info(`第 ${++runCount} 次数据抓取开始运行。`);
    getDataIds(url).then((ids) => {
        readyCount = ids.size;
        ids.forEach((t) => {
            readTyphoonData(t);
        });
    }).catch((e) => {
        console.warn('请求失败。错误信息：');
        console.warn(e);
        setTimeout(run, delay);
    });
};

run();
